﻿@using System.Globalization
@model  Model.ModelConfig
@{
    ViewBag.Title = "编辑模型信息";
    Layout = "~/Views/Shared/_Main.cshtml";
}
@section Head{
    <style>
        .Reference
        {
            display: none;
        }
        #t2
        {
            display: none;
        }
        .version, .modelEdit
        {
            display: none;
        }
    </style>
}
<script type="text/javascript">
    var UrlArray = $.getUrlArray();
    var modelTypeList = new Array();
    var modelRef = "@Model.ParentModelID";
    var opts = {
        rows: 15,
        height: 450,
        para: { dbName: encodeURI(UrlArray["dbName"]), version: UrlArray["version"] },
        idField: "GUID",
        columns: [
                    { field: 'DefaultView', title: '名称', width: 40 },
                    { field: 'TableDescription', title: '描述', width: 40 },
                    { field: 'CreateTime', title: '创建时间', width: 10, align: 'center' },
                    { field: 'GUID', title: '操作', width: 10, align: 'center', formatter: function (value, rowData, rowIndex) {
                        var add = true; ;
                        for (var j = 0; j < modelTypeList.length; j++) {
                            if (modelTypeList[j].GUID == rowData.GUID) {
                                add = false;
                            }
                        }
                        if (add) {
                            modelTypeList.push(rowData);
                        }
                        return "<a href='#' onclick='remove(\"" + rowIndex + "\")'>删除</a>";
                    }
                    }
                    ]
    };
    var urlPara;
    $(function () {
        urlPara = "?modelID=" + UrlArray["modelID"] + "&dbName=" + encodeURI(UrlArray["dbName"]);
        $("#link a").each(function () {
            $(this).attr("href", $(this).attr("href") + urlPara);
        })

        if (UrlArray["version"]) {
            jv.SetDetail(".searchArea");
            $(".modelEdit").hide();
            $(".version").show();
            urlPara = urlPara + "&version=" + UrlArray["version"];
        } else
            $(".modelEdit").show();
        if (UrlArray["modelID"]) {
            opts.url = "/ModelConfig/GetTypeByModelID?modelID=" + '@Model.GUID' + "&type=dict&dbName=" + encodeURI(UrlArray["dbName"]);
            $("#DataGrid").dg(opts);
            $("#selIsRecord").val($.trim('@Model.IsRecord'));

        } else {
            $(".editLink").hide();
        }

        if ('@Model.AddType' == "Reference") {
            $(".Reference").show();
            opts.url = "/ModelConfig/GetType?GUID=" + '@Model.ParentModelID' + "&type=model&dbName=" + encodeURI(UrlArray["dbName"]);
            $("#DataGrid_m").dg(opts);
        } else if ('@Model.AddType' == "Copy") {
            $("#t2").show();
            $("#t1").hide();
        }
    })
    function InitList() {
        $("#DataGrid").dg(opts);
    }
    function AddItem() {
        showModalMany('Contact', '/ModelConfig/ModelList?type=1&selectCount=-1&dbName=' + encodeURI(UrlArray["dbName"]), '800', '480', returnFun);
    }
    function AddModel() {
        showModalMany('Contact', '/ModelConfig/ModelList?reference=1&type=&dbName=' + encodeURI(UrlArray["dbName"]), '850', '480', returnModel);
    }
    function remove(i) {
        if (confirm("确定要删除吗？")) {
            modelTypeList.removeAt(i);
            var data = { "total": 0, "rows": modelTypeList };
            opts["data"] = data;
            $("#DataGrid").dg(opts);
        }
    }
    function returnFun(v) {
        for (var i = 0; i < v.length; i++) {
            var has = false;
            for (var j = 0; j < modelTypeList.length; j++) {
                if (v[i].GUID == modelTypeList[j].GUID) {
                    has = true;
                    break;
                }
            }
            if (has == false) {
                modelTypeList.push(v[i]);
            }
        }
        var data = { "total": 1, "rows": modelTypeList };
        opts["data"] = data;
        $("#DataGrid").dg(opts);
    }
    function returnModel(v) {
        modelRef = v[0].GUID;
        v[0].AddType = "Reference";
        var data = { "total": 1, "rows": v };
        opts["data"] = data;
        $("#DataGrid_m").dg(opts);
    }
    function back() {
        window.location = "/ModelConfig/Index?dbName=" + encodeURI(UrlArray["dbName"]);
    }
    var modelID = "";
    function Save(t) {
        var addType = $('input[name="radType"]:checked').val();
        if ($.trim($("#txtTableName").val()) == "") {
            alert("模型名称不能为空。");
            return;
        }
        var typeIDs = new Array()
        for (var j = 0; j < modelTypeList.length; j++) {
            typeIDs.push(modelTypeList[j].GUID);
        }
        if (modelID == "")
            modelID = "@Model.GUID";
        var para = {
            id: modelID,
            Name: $("#txtTableName").val(),
            Description: $("#txtDescription").val(),
            IsRecord: $("#selIsRecord").val(),
            AddType: addType,
            ModelTypeID: typeIDs.join(","),
            ModelRefID: modelRef,
            dbName: encodeURI(UrlArray["dbName"])
        };
        $("#loading").show();
        $.post("/ModelConfig/SaveModelData", para, function (result) {
            if (result[0] == "ok") {
                if (t == "save") {
                    modelID = result[1];
                    alert("操作成功！");
                }
                else {
                    if (addType == "Add") {
                        window.location = "/ODSConfig/ODSConfigAdd?modelID=" + result[1] + "&dbName=" + encodeURI(UrlArray["dbName"]);
                    }
                    else if (addType == "Reference") {
                        window.location = "/ModelConfig/EditColumn?modelID=" + result[1] + "&dbName=" + encodeURI(UrlArray["dbName"]);
                    }
                    else {
                        window.location = "/ModelConfig/End?modelID=" + result[1] + "&dbName=" + encodeURI(UrlArray["dbName"]);
                    }
                }
            }
            else
                alert(result);

            $("#loading").hide();
        });
    }
    function checkAddType(t) {
        if (t == "reference")
            $(".Reference").show();
        else
            $(".Reference").hide();
    }
    function gotoUrl(v) {
        window.location = v + urlPara;
    }
</script>
<div style="margin: 5px;">
    <table class="listTitle" id="link">
        <tr>
            <td width="15">
                <img src="/images/edit.png" />
            </td>
            <td width="300">
                <div class="version">
                    <a href="/ModelConfig/Index">模型管理</a> --> 查看模型渐变维度 --> 模型基本信息配置
                </div>
                <div class="modelEdit">
                    <a href="/ModelConfig/Index">模型管理</a> --> 编辑模型配置
                </div>
            </td>
            <td align="right">
                <div class="version">
                    <input type="button" class="button" value="查看模型字段" onclick="gotoUrl('/ModelConfig/EditColumn')" />
                    <input type="button" class="button" value="查看模型扩展字段" onclick="gotoUrl('/ModelConfig/ExtendEdit')"
                        style="width: 125px;" />
                    <input type="button" class="button" value="返回" onclick="gotoUrl('/ModelConfig/ConfigRecordList')" />
                </div>
                <div class="modelEdit editLink">
                    <input type="button" class="button" value="编辑数据源表" onclick="gotoUrl('/ODSConfig/ODSConfigAdd')" />
                    <input type="button" class="button" value="编辑数据质量" onclick="gotoUrl('/DQConfig/Index')" />
                    <input type="button" class="button" value="编辑模型字段" onclick="gotoUrl('/ModelConfig/EditColumn')" />
                    <input type="button" class="button" value="编辑模型扩展字段" onclick="gotoUrl('/ModelConfig/ExtendEdit')"
                        style="width: 125px;" />
                </div>
            </td>
        </tr>
    </table>
    <table class="searchArea" width="100%">
        <tr>
            <td valign="top" height="500">
                <table>
                    <tr>
                        <td width="80" align="right">
                            <span class="red">*</span>模型名称：
                        </td>
                        <td>
                            <input type="text" id="txtTableName" value="@Model.TableName" style="width: 400px" />
                        </td>
                    </tr>
                    <tr>
                        <td align="right" valign="top">
                            模型描述：
                        </td>
                        <td>
                            <textarea id="txtDescription" style="width: 400px; height: 50px;">@Model.TableDescription</textarea>
                        </td>
                    </tr>
                    <tr>
                        <td align="right" valign="top">
                            版本记录：
                        </td>
                        <td>
                            <select id="selIsRecord">
                                <option value="0">禁用</option>
                                <option value="1">启用</option>
                            </select>
                        </td>
                    </tr>
                    <tr style="display: none;">
                        <td align="right" valign="top">
                            模型来源：
                        </td>
                        <td>
                            <div id="t1">
                                <span>
                                    <input type="radio" onclick="checkAddType('add')" name="radType" value="Add" class="rad"
                                        checked='checked' /></span><span> 新建数据源</span><br>
                                <span></span><span>新建数据源是按流程：录入模型信息--编辑数据源--编辑数据质量--编辑模型字段--编辑模型扩展字段--保存执行。</span>
                            </div>
                            <div id="t2">
                                <input type="radio" class="rad" name="radType" value="Copy" @(Model.AddType == "Copy" ? "checked='checked'" : "") />
                                复制模型<br>
                                复制模型是复制其他模型所有配置，简化操作流程。
                            </div>
                        </td>
                    </tr>
                </table>
                <div class="Reference">
                    <table class="listTitle">
                        <tr>
                            <td width="15">
                                <img src="/images/edit.png" />
                            </td>
                            <td width="300">
                                引用模型
                            </td>
                            <td align="right">
                                <input type="button" value="选择模型" onclick="AddModel();" class="button" />
                            </td>
                        </tr>
                    </table>
                    <table id="DataGrid_m" class="pagTable" cellpadding="0" cellspacing="0">
                    </table>
                </div>
                <table class="listTitle">
                    <tr>
                        <td width="15">
                            <img src="/images/edit.png" />
                        </td>
                        <td width="300">
                            模型标签
                        </td>
                        <td align="right">
                            <div class="modelEdit">
                                <input type="button" value="添加标签" onclick="AddItem();" class="button" />
                            </div>
                        </td>
                    </tr>
                </table>
                <table id="DataGrid" class="pagTable" cellpadding="0" cellspacing="0">
                </table>
            </td>
        </tr>
    </table>
    <div class="modelEdit">
        <input type="button" class="button" value="返回" onclick="back()" />
        <input type="button" class="button" value="保存" onclick="Save('save')" />
        <input type="button" class="button" value="下一步" onclick="Save('')" />
    </div>
    <div id="msg">
    </div>
    <div id="loading" class="loading">
        <img src="/Images/tree_loading.gif" />
    </div>
</div>
